﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Web.UI;
using AMS.Core.Entities;
using AMS.History.DataAccess;
using AMS.History.Entities;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace AMS.Web.BaseData.Helper
{
    public class AMSPageSaver : PageSaveHelper
    {
        public AMSPageSaver(Page page, string settingXmlPath) : base(page, settingXmlPath)
        {
            m_Page = page;
            m_RuleHelper = new PageRuleHelper(settingXmlPath);
            m_LoadHelper = new PageLoadHelper(settingXmlPath);
            m_XmlReader = new ObjXmlReader(settingXmlPath);
            m_PageBuilder = new PageBuilder(m_Page, settingXmlPath);
        }

        public override object SavePageObject(string className, string typeName, string keyValue,
                                              out StringCollection scObjErrMsg, bool isTrans, bool isHistory)
        {
            object obj;
            var acc = new BaseDataAccess();
            var hisAcc = new HistoryAccess();
            hisAcc.Session = acc.Session;
            if (isTrans)
                acc.BeginTransaction();
            try
            {
                string strClassMemo = m_XmlReader.GetNodeAttributeValue(className, m_LoadHelper.MEMO, true);
                string strKeyField = m_LoadHelper.SetObjectKeyField(className);
                bool isAdd = true;
                obj = GetPageObject(className, typeName, keyValue, acc, out isAdd);
                scObjErrMsg = new StringCollection();
                m_RuleHelper.AccSession = acc.Session;
                string strHisMemo = strClassMemo + "页面新增";

                m_RuleHelper.CheckObjectSaveDBRules(obj, out scObjErrMsg);
                if (isAdd)
                {
                    if (scObjErrMsg.Count == 0)
                    {
                        if (DemoVersionManager.IsDemo())
                        {
                            // 试用版检查，如果系统资产数大于60，则试用版不能满足要求,如果不是试用版则去掉此代码
                            if (DemoVersionManager.CheckIsDemoVersion(className, scObjErrMsg))
                            {
                                return null;
                            }
                            else
                            {
                                if (className == "AssetInfo")
                                {
                                    IList barcodeFormatList = acc.GetObjectList(new BarcodeFormat().GetType().Name, "AssetTypeName",
                                                                                typeName);
                                    if (barcodeFormatList != null && barcodeFormatList.Count > 0)
                                    {
                                        var barcodeFormat = (BarcodeFormat)barcodeFormatList[0];
                                        var assetInfo = (AssetInfo)obj;
                                        assetInfo.BarcodeFormat = barcodeFormat;
                                        obj = assetInfo;
                                    }

                                }
                                acc.Add(obj);
                            } 
                        }
                        else
                        {
                            if (className == "AssetInfo")
                            {
                                IList barcodeFormatList = acc.GetObjectList(new BarcodeFormat().GetType().Name, "AssetTypeName",
                                                                            typeName);
                                if (barcodeFormatList != null && barcodeFormatList.Count > 0)
                                {
                                    var barcodeFormat = (BarcodeFormat)barcodeFormatList[0];
                                    var assetInfo = (AssetInfo)obj;
                                    assetInfo.BarcodeFormat = barcodeFormat;
                                    obj = assetInfo;
                                }

                            }
                            acc.Add(obj);
                        }
                     
                    }

                        
                    else
                        return null;
                }
                else
                {
                    if (obj.GetType() == new BarcodeFormat().GetType())
                    {
                        var barcodeFormat = (BarcodeFormat) obj;
                        string strType = barcodeFormat.AssetTypeName;
                        IList AssetList = acc.GetObjectList(new AssetInfo().GetType().Name, "TypeName", strType);
                        if (AssetList != null && AssetList.Count > 0)
                        {
                            scObjErrMsg.Add("此条码格式已经被资产使用，不能修改！");
                        }
                    }
                    strHisMemo = strClassMemo + "页面更新";
                    if (scObjErrMsg.Count == 0)
                        acc.Update(obj);
                    else
                        return null;
                    
                }
                if (isHistory)
                {
                    MethodInfo mi = obj.GetType().GetMethod(BaseHistoryEntity.GET_HISTORY_OBJECT_TYPE);
                    if (mi != null)
                    {
                        object miReVal = mi.Invoke(obj, null);
                        if (miReVal != null && miReVal is Type)
                            hisAcc.SaveHisRecord(obj, (Type) miReVal, strKeyField, strHisMemo);
                    }
                }
                if (isTrans)
                    acc.Commit();
            }
            catch (Exception ex)
            {
                if (isTrans)
                {
                    scObjErrMsg = new StringCollection { "某些值必须为数字类型，请检测！" };
                    acc.RollBack();
                    return null;
                }
                throw;
            }
            finally
            {
                if (isTrans)
                    acc.CloseSession();
            }
            return obj;
        }
 
    }
}